<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {getCount} from "@/api/dashboard/chart";

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      let _this = this;
      getCount().then((response) => {
        _this.chart.setOption({
          title: {
            text: '年龄分布',
            left: 'right',
            top: -4,
            textStyle: {
              color: '#06a7f3'
            }
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          xAxis: {
            type: 'category',
            data: ['30以下', '30-35', '35-40', '40-45', '45-50', '50-55', '55-60']
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              data: response.data.numbers,
              type: 'bar',
              showBackground: true,
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
              }
            }
          ]
        })
      });


    }
  }
}
</script>
